define([
    'jquery',
    'bbcare',
    'scprit/followup/app/breastSupply/breastSupply.server.js'
], function($, $bb, $servers) {
    var supply = {
        table: {
            current: 1,
            pageSize: 10,
            total: 0,
            queueId: '',
        }
    }

    $('.content-main').on('click', '._breastSupply_update_block', function() {
        $('._breastSupply_update_input').click()
    })

    $('.content-main').on('change', '._breastSupply_update_input', function (e) {
        var file = ''
        try {
            file = e.target.files[0]
        } catch(err) {
            file = ''
        }
        if (file === '') return
        if (file.type.indexOf('image') === -1) {
            e.target.value = null
            return $bb.toastShow('请选择图片文件')
        }
        var param = {
            file: file
        }
        loading('上传中...')
        $servers.ajaxPolicy(param, ajaxPolicyCallBack)
    })

    var ajaxPolicyCallBack = function (param, result) {
        var fd = new FormData()
        fd.append('key', result.dir + param.file.name)
        fd.append('policy', result.policy)
        fd.append('OSSAccessKeyId', result.accessId)
        fd.append('success_action_status', '200')
        fd.append('Signature', result.signature)
        fd.append("file",param.file);
        var param = {
            url: result.host,
            fd: fd,
            imgUrl: result.host + result.dir + param.file.name,
        }
        $servers.updateImage(param, updateImageCallback)
    }

    var sendUrlToServerCallback = function (param, result) {
        removeLoading('test')
        $bb.toastShow('上传成功')
        getUpdateList()
    }

    var updateImageCallback = function (param, result) {
        var obj = {
            url: param.imgUrl
        }
        $servers.sendUrlToServer(obj, sendUrlToServerCallback)
    }

    var getUpdateListCallBack = function (param, result) {
        supply.table.total = Math.ceil(result.count / 10)
        renderTable(result.data)
    }

    var renderTable = function (data) {
        $('.created').remove()
        $('._breastSupply_table_none').hide()
        var html = ''
        for (var i = 0; i < data.length; i++) {
            html += '<tr class="created">' +
                        '<td>'+ data[i].uploadTime.split(' ')[0] +'</td>' + 
                        '<td>'+ data[i].uploadTime.split(' ')[1] +'</td>' +
                        '<td>'+ data[i].auth +'</td>' +
                        '<td>' +
                            '<span class="unread" data-id="'+ data[i].id +'">查看未读用户</span>' + 
                            '<span class="check" data-url="'+ data[i].url +'">查看上传文件</span>' +
                        '</td>' +
                    '</tr>'
        }
        $('._breastSupply_table').append(html)
        $('._pageCtrl_count').text(supply.table.total)
        removeLoading('test')
        if (data.length === 0) $('._breastSupply_table_none').show()
    }

    var getUpdateList = function () {
        var param = {
            currentPage: supply.table.current,
            pageSize: supply.table.pageSize
        }
        loading('加载中...')
        $servers.getUpdateList(param, getUpdateListCallBack)
    }

    /**
     * 点击页脚的页码按钮
     */
    $('.content-main').on('click', '._btns_item', function (e) {
        var type = $(this).data('type')
        switch(type) {
            case 'prev':
                if (supply.table.current === 1) return 
                supply.table.current-- 
            break
            case 'next':
                if (supply.table.current === supply.table.total) return 
                supply.table.current++
            break
            case 'jump':
                var val = Number($('._pageCtrl_input').val())
                // 如果输入的不是数字时
                if (val !== val) {
                    return $('._pageCtrl_input').val(supply.table.current)
                }
                if (val > supply.table.total) {
                    supply.table.current = supply.table.total
                } else {
                    supply.table.current = val
                }
            break
        }
        $('._pageCtrl_input').val(supply.table.current)
        getUpdateList()
    })

    /**
     * 点击查看未读通知
     */
    $('.content-main').on('click', '.unread', function () {
        var id = $(this).data('id')
        var param = {
            id: id
        }
        $servers.getUnreadList(param, getUnreadListCallback)
    })

    $('.content-main').on('click', '.unreadList_close', function () {
        $('.unreadList').hide()
    })

    var getUnreadListCallback = function (param, result) {
        $('.unreadList_content_rows').empty()
        $('.unreadList_none').hide()
        var html = ''
        for (var i = 0; i < result.length; i++) {
            html += '<div class="content_block_row">' + 
                        '<span>'+ result[i].Pub1100002 +'</span>' + 
                        '<span>'+ (result[i].zcejd_ch ? result[i].zcejd_ch : '未填写') +'</span>' + 
                        '<span>'+ (result[i].Pub1100001 ? result[i].Pub1100001 : '未填写') +'</span>' + 
                    '</div>'
        }
        $('.unreadList_content_rows').append(html)
        $('.unreadList').show()
        if (result.length === 0) $('.unreadList_none').show()
    }

    supply.init = function () {
        getUpdateList()
        $('.content-main').on('click', '.check', function () {
            $('.preview').empty()
            var url = $(this).data('url')
            $('.preview').append('<img src="' + url + '" />')
            $('.preview').show()
        })
        $('.preview').click(function () {
            $(this).hide()
        })
    }

    function loading(text) {
        $('body').loading({
            loadingWidth:240,
            title:'请稍等!',
            name:'test',
            discription: text,
            direction:'column',
            type:'origin',
            // originBg:'#71EA71',
            originDivWidth:40,
            originDivHeight:40,
            originWidth:6,
            originHeight:6,
            smallLoading:false,
            loadingMaskBg:'rgba(0,0,0,0.2)'
        });
    }

    return supply

});

